 function finddet(N, mat) result(det)
!complex(kind=8) function det(N, mat)
     implicit none
!     integer(kind=8), intent(in) :: N 
     integer, intent(in) :: N 
     real(kind=8), intent(in), dimension(1:N,1:N) :: mat
     real(kind=8) :: det
!      complex(kind=8), intent(inout), dimension(:,:) :: mat
!     integer(kind=8) :: i, info
     integer :: i, info
     integer, allocatable :: ipiv(:)
     real(kind=8) :: sgn

     allocate(ipiv(N))
     ipiv = 0

     call dgetrf(N, N, mat, N, ipiv, info)
!     call zgetrf(2, 2, mat, 2, ipiv, info)

     det = 1
     write(*,*)' det=',det
     do i = 1, N
         det = det*mat(i, i)
     end do

     sgn = 1
     do i = 1, N
         if(ipiv(i) /= i) then
            sgn = -sgn
         end if
     end do

     det = sgn*det   

end function finddet
